package com.paipaideli.ui.mine.order;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.ui.mine.order.fragment.AllFragment;
import com.paipaideli.ui.mine.order.fragment.FailFragment;
import com.paipaideli.ui.mine.order.fragment.PayFragment;
import com.paipaideli.ui.mine.order.fragment.ReceiveFragment;
import com.paipaideli.ui.mine.order.fragment.ShipFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

/**
 * Created by 我的订单
 */
public class OrderActivity extends BasePresenterActivity {

    private AllFragment allFragment;
    private PayFragment payFragment;
    private ShipFragment shipFragment;
    private ReceiveFragment receiveFragment;
    private FailFragment failFragment;

    @BindViews({R.id.ll_all_order, R.id.ll_pay_order, R.id.ll_ship_order, R.id.ll_receive_order, R.id.ll_fali_order})
    List<LinearLayout> linearLayout;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;

    private String whichOrder;

    @Override
    public int getContentView() {
        return R.layout.activity_order;
    }

    @Override
    public void initView() {
        whichOrder = getIntent().getStringExtra("whichOrder");
        if (whichOrder.equals("1")){
            selected(R.id.ll_all_order);
        }else if (whichOrder.equals("2")){
            selected(R.id.ll_pay_order);
        }else if (whichOrder.equals("3")){
            selected(R.id.ll_ship_order);
        }else if (whichOrder.equals("4")){
            selected(R.id.ll_receive_order);
        }else {
            selected(R.id.ll_fali_order);
        }

        top_title.setText("订单信息");
        top_back.setOnClickListener(o ->{
            finish();
        });
    }

    @OnClick({R.id.ll_all_order, R.id.ll_pay_order, R.id.ll_ship_order, R.id.ll_receive_order, R.id.ll_fali_order})
    public void onItemClick(View view) {
        switch (view.getId()) {
            case R.id.ll_all_order:
                selected(R.id.ll_all_order);
                break;
            case R.id.ll_pay_order:
                selected(R.id.ll_pay_order);
                break;
            case R.id.ll_ship_order:
                selected(R.id.ll_ship_order);
                break;
            case R.id.ll_receive_order:
                selected(R.id.ll_receive_order);
                break;
            case R.id.ll_fali_order:
                selected(R.id.ll_fali_order);
                break;
        }

    }

    public void selected(int id) {
        for (LinearLayout views : linearLayout) {
            if (views.getId() == id) {
                views.setSelected(true);
                initFragment(id);
            } else {
                views.setSelected(false);
            }
        }
    }

    //显示fragment
    public void initFragment(int id) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragments(transaction);
        switch (id) {
            case R.id.ll_all_order:
                if (allFragment == null) {
                    allFragment = AllFragment.newInstance();
                    transaction.add(R.id.fragment_container, allFragment);
                }
                transaction.show(allFragment);
                break;
            case R.id.ll_pay_order:
                if (payFragment == null) {
                    payFragment = PayFragment.newInstance();
                    transaction.add(R.id.fragment_container, payFragment);
                }
                transaction.show(payFragment);
                break;
            case R.id.ll_ship_order:
                if (shipFragment == null) {
                    shipFragment = ShipFragment.newInstance();
                    transaction.add(R.id.fragment_container, shipFragment);
                }
                transaction.show(shipFragment);
                break;
            case R.id.ll_receive_order:
                if (receiveFragment == null) {
                    receiveFragment = ReceiveFragment.newInstance();
                    transaction.add(R.id.fragment_container, receiveFragment);
                }
                transaction.show(receiveFragment);
                break;
            case R.id.ll_fali_order:
                if (failFragment == null) {
                    failFragment = FailFragment.newInstance();
                    transaction.add(R.id.fragment_container, failFragment);
                }
                transaction.show(failFragment);
                break;
            default:
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    //切换
    private void hideFragments(FragmentTransaction transaction) {
        if (allFragment != null) {
            transaction.hide(allFragment);
        }
        if (payFragment != null) {
            transaction.hide(payFragment);
        }
        if (shipFragment != null) {
            transaction.hide(shipFragment);
        }
        if (receiveFragment != null) {
            transaction.hide(receiveFragment);
        }
        if (failFragment != null) {
            transaction.hide(failFragment);
        }
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        if (allFragment == null && fragment instanceof AllFragment)
            allFragment = (AllFragment) fragment;
        if (payFragment == null && fragment instanceof PayFragment)
            payFragment = (PayFragment) fragment;
        if (shipFragment == null && fragment instanceof ShipFragment)
            shipFragment = (ShipFragment) fragment;
        if (receiveFragment == null && fragment instanceof ReceiveFragment)
            receiveFragment = (ReceiveFragment) fragment;
        if (failFragment == null && fragment instanceof FailFragment)
            failFragment = (FailFragment) fragment;
    }
}
